<template><div><h2 id="数据库-分页" tabindex="-1"><a class="header-anchor" href="#数据库-分页"><span>数据库：分页</span></a></h2>
<ul>
<li><a href="#%E4%BB%8B%E7%BB%8D">介绍</a></li>
<li><a href="#%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95">基本用法</a>
<ul>
<li><a href="#%E5%AF%B9%E6%9F%A5%E8%AF%A2%E6%9E%84%E9%80%A0%E5%99%A8%E7%BB%93%E6%9E%9C%E8%BF%9B%E8%A1%8C%E5%88%86%E9%A1%B5">对查询构造器结果进行分页</a></li>
<li><a href="#EloquentORM%E5%88%86%E9%A1%B5">Eloquent ORM 分页</a></li>
<li><a href="#%E6%B8%B8%E6%A0%87%E5%88%86%E9%A1%B5">游标分页</a></li>
<li><a href="#%E6%89%8B%E5%8A%A8%E5%88%9B%E5%BB%BA%E5%88%86%E9%A1%B5">手动创建分页</a></li>
<li><a href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E5%88%86%E9%A1%B5URL">自定义分页 URL</a></li>
</ul>
</li>
<li><a href="#%E6%98%BE%E7%A4%BA%E5%88%86%E9%A1%B5%E7%BB%93%E6%9E%9C">显示分页结果</a>
<ul>
<li><a href="#%E8%B0%83%E6%95%B4%E5%88%86%E9%A1%B5%E9%93%BE%E6%8E%A5%E7%AA%97%E5%8F%A3">调整分页链接窗口</a></li>
<li><a href="#%E5%B0%86%E7%BB%93%E6%9E%9C%E8%BD%AC%E6%8D%A2%E4%B8%BAJSON">将结果转换为 JSON</a></li>
</ul>
</li>
<li><a href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E5%88%86%E9%A1%B5%E8%A7%86%E5%9B%BE">自定义分页视图</a>
<ul>
<li><a href="#%E4%BD%BF%E7%94%A8Bootstrap">使用 Bootstrap</a></li>
</ul>
</li>
<li><a href="#%E5%88%86%E9%A1%B5%E5%99%A8%E5%AE%9E%E4%BE%8B%E6%96%B9%E6%B3%95">分页器实例方法</a></li>
<li><a href="#%E6%B8%B8%E6%A0%87%E5%88%86%E9%A1%B5%E5%99%A8%E5%AE%9E%E4%BE%8B%E6%96%B9%E6%B3%95">游标分页器实例方法</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p>在其他框架中，分页可能非常痛苦，我们希望 Laravel 的分页方法像一股新鲜空气。 Laravel 的分页器集成了 <a href="https://learnku.com/docs/laravel/10.x/queries" target="_blank" rel="noopener noreferrer">query builder</a> 和 <a href="https://learnku.com/docs/laravel/10.x/eloquent" target="_blank" rel="noopener noreferrer">Eloquent ORM</a>，并提供了方便、易于使用的无需任何配置的数据库记录分页。</p>
<p>默认情况下，由分页器生成的 HTML 与 <a href="https://tailwindcss.com/" target="_blank" rel="noopener noreferrer">Tailwind CSS 框架</a> 兼容，然而，引导分页支持也是可用的。</p>
<h4 id="tailwind-jit" tabindex="-1"><a class="header-anchor" href="#tailwind-jit"><span>Tailwind JIT</span></a></h4>
<p>如果你使用 Laravel 的默认 Tailwind 视图和 Tailwind JIT 引擎，你应该确保你的应用程序的 <code v-pre>tailwind.config.js</code> 文件的 <code v-pre>content</code> 关键引用 Laravel 的分页视图，这样它们的 Tailwind 类就不会被清除：</p>
<div class="language-javascript line-numbers-mode" data-highlighter="prismjs" data-ext="js" data-title="js"><pre v-pre class="language-javascript"><code><span class="line"><span class="token literal-property property">content</span><span class="token operator">:</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string">'./resources/**/*.blade.php'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string">'./resources/**/*.js'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string">'./resources/**/*.vue'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string">'./vendor/laravel/framework/src/Illuminate/Pagination/resources/views/*.blade.php'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="基础用法" tabindex="-1"><a class="header-anchor" href="#基础用法"><span>基础用法</span></a></h2>
<h3 id="对查询构造器结果进行分页" tabindex="-1"><a class="header-anchor" href="#对查询构造器结果进行分页"><span>对查询构造器结果进行分页</span></a></h3>
<p>有几种方法可以对结果进行分页，最简单的方法是在 <a href="https://learnku.com/docs/laravel/10.x/queries" target="_blank" rel="noopener noreferrer">query builder</a> 或 <a href="https://learnku.com/docs/laravel/10.x/eloquent" target="_blank" rel="noopener noreferrer">Eloquent query</a> 上使用 <code v-pre>paginate</code> 方法， <code v-pre>paginate</code> 方法根据用户查看的当前页面自动设置查询的「limit」和「offset」，默认情况下，通过 HTTP 请求中的 <code v-pre>page</code> 查询字符串参数的值检测当前页面，Laravel会自动检测这个值，它也会自动插入到分页器生成的链接中。</p>
<p>在下面的例子中，传递给 <code v-pre>paginate</code> 方法的唯一参数是你想要在一页中显示的记录数。在此例中，我们希望「每页」显示 <code v-pre>15</code> 条数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>DB</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>View<span class="token punctuation">\</span>View</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">UserController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 显示应用中所有用户列表</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">index</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">View</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user.index'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'users'</span> <span class="token operator">=></span> <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="简单分页" tabindex="-1"><a class="header-anchor" href="#简单分页"><span>简单分页</span></a></h4>
<p>该 <code v-pre>paginate</code> 方法会在查询数据库之前先计算与查询匹配的记录总数，从而让分页器知道总共需要有多少个页面来显示所有的记录。不过，如果你不打算在界面上显示总页数的话，那么计算记录总数是没有意义的。</p>
<p>因此，如果你只需要显示一个简单的「上一页」和「下一页」链接的话， <code v-pre>simplePaginate</code> 方法是一个更高效的选择：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">simplePaginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="eloquent-orm-分页" tabindex="-1"><a class="header-anchor" href="#eloquent-orm-分页"><span>Eloquent ORM 分页</span></a></h3>
<p>你也可以对 <a href="https://learnku.com/docs/laravel/10.x/eloquent" target="_blank" rel="noopener noreferrer">Eloquent</a> 查询结果进行分页. 在下面的例子中，我们将 <code v-pre>App\Models\User</code> 模型按每页 15 条记录进行分页。如你所见，其语法与查询构造器分页基本相同：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当然，你也可以在调用 <code v-pre>paginate</code> 方法之前为查询添加其他约束，例如 <code v-pre>where</code> 子句：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你也可以在 Eloquent ORM 分页中使用 <code v-pre>simplePaginate</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">simplePaginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>同样，您可以使用 <code v-pre>cursorPaginate</code> 方法对 Eloquent 模型进行游标分页：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">cursorPaginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="每页有多个-paginator-实例" tabindex="-1"><a class="header-anchor" href="#每页有多个-paginator-实例"><span>每页有多个 Paginator 实例</span></a></h4>
<p>有时你可能需要在应用程序呈现的单个屏幕上呈现两个单独的分页器。 但是，如果两个分页器实例都使用 <code v-pre>page</code> 查询字符串参数来存储当前页面，则两个分页器会发生冲突。 要解决此冲突，您可以通过提供给 <code v-pre>paginate</code>、<code v-pre>simplePaginate</code> 和 <code v-pre>cursorPaginate</code> 方法的第三个参数传递你希望用于存储分页器当前页面的查询字符串参数的名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">paginate</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token variable">$perPage</span> <span class="token operator">=</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token variable">$columns</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'*'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$pageName</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'users'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="游标分页" tabindex="-1"><a class="header-anchor" href="#游标分页"><span>游标分页</span></a></h3>
<p>虽然 <code v-pre>paginate</code> 和 <code v-pre>simplePaginate</code> 使用 SQL「offset」 子句创建查询，但游标分页通过构造「where」子句来工作，这些子句比较查询中包含的有序列的值，提供所有可用的最有效的数据库性能 Laravel 的分页方法。 这种分页方法特别适合大型数据集和「无限」滚动用户界面。</p>
<p>与基于偏移量的分页在分页器生成的 URL 的查询字符串中包含页码不同，基于游标的分页在查询字符串中放置一个「游标」字符串。游标是一个编码字符串，包含下一个分页查询应该开始分页的位置和它应该分页的方向：</p>
<div class="language-nothing line-numbers-mode" data-highlighter="prismjs" data-ext="nothing" data-title="nothing"><pre v-pre class="language-nothing"><code><span class="line">http://localhost/users?cursor=eyJpZCI6MTUsIl9wb2ludHNUb05leHRJdGVtcyI6dHJ1ZX0</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以通过查询生成器提供的 <code v-pre>cursorPaginate</code> 方法创建基于游标的分页器实例。这个方法返回一个 <code v-pre>Illuminate\Pagination\CursorPaginator</code> 的实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">cursorPaginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>检索到游标分页器实例后，你可以像使用 <code v-pre>paginate</code> 和 <code v-pre>simplePaginate</code> 方法时一样<a href="https://learnku.com/docs/laravel/10.x/pagination/displaying-pagination-results" target="_blank" rel="noopener noreferrer">显示分页结果</a>。更多游标分页器提供的实例方法请参考<a href="#cursor-paginator-instance-methods">游标分页器实例方法文档</a>.</p>
<blockquote>
<p><strong>注意</strong><br>
你的查询必须包含「order by」子句才能使用游标分页。</p>
</blockquote>
<h4 id="游标与偏移分页" tabindex="-1"><a class="header-anchor" href="#游标与偏移分页"><span>游标与偏移分页</span></a></h4>
<p>为了说明偏移分页和游标分页之间的区别，让我们检查一些示例 SQL 查询。 以下两个查询都将显示按 <code v-pre>id</code> 排序的 <code v-pre>users</code> 表的「第二页」结果：</p>
<div class="language-sql line-numbers-mode" data-highlighter="prismjs" data-ext="sql" data-title="sql"><pre v-pre class="language-sql"><code><span class="line"><span class="token comment"># 偏移分页...</span></span>
<span class="line"><span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> users <span class="token keyword">order</span> <span class="token keyword">by</span> id <span class="token keyword">asc</span> <span class="token keyword">limit</span> <span class="token number">15</span> <span class="token keyword">offset</span> <span class="token number">15</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment"># 游标分页...</span></span>
<span class="line"><span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> users <span class="token keyword">where</span> id <span class="token operator">></span> <span class="token number">15</span> <span class="token keyword">order</span> <span class="token keyword">by</span> id <span class="token keyword">asc</span> <span class="token keyword">limit</span> <span class="token number">15</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>与偏移分页相比，游标分页查询具有以下优势：</p>
<ul>
<li>
<p>对于大型数据集，如果「order by」列被索引，游标分页将提供更好的性能。 这是因为「offset」子句会扫描所有先前匹配的数据。</p>
</li>
<li>
<p>对于频繁写入的数据集，如果最近在用户当前查看的页面中添加或删除了结果，偏移分页可能会跳过记录或显示重复。</p>
</li>
</ul>
<p>但是，游标分页有以下限制：</p>
<ul>
<li>
<p>与 <code v-pre>simplePaginate</code> 一样，游标分页只能用于显示「下一个」和「上一个」链接，不支持生成带页码的链接。</p>
</li>
<li>
<p>它要求排序基于至少一个唯一列或唯一列的组合。 不支持具有 <code v-pre>null</code> 值的列。</p>
</li>
<li>
<p>「order by」子句中的查询表达式仅在它们被别名并添加到「select」子句时才受支持。</p>
</li>
<li>
<p>不支持带参数的查询表达式。</p>
</li>
</ul>
<h3 id="手动创建分页" tabindex="-1"><a class="header-anchor" href="#手动创建分页"><span>手动创建分页</span></a></h3>
<p>有时你可能希望手动创建分页，并传递一个包含数据的数组给它。这可以通过手动创建 <code v-pre>Illuminate\Pagination\Paginator</code>, <code v-pre>Illuminate\Pagination\LengthAwarePaginator</code> 或者 <code v-pre>Illuminate\Pagination\CursorPaginator</code> 实例来实现，这取决于你的需要。</p>
<p><code v-pre>Paginator</code> 不需要知道数据的总数。然而，你也无法通过 <code v-pre>Paginator</code> 获取最后一页的索引。而 <code v-pre>LengthAwarePaginator</code> 接受和 <code v-pre>Paginator</code> 几乎相同的参数，不过，它会计算数据的总数。</p>
<p>或者说，<code v-pre>Paginator</code> 相当于查询构造器或者 Eloquent ORM 分页的 <code v-pre>simplePaginate</code> 方法，而 <code v-pre>LengthAwarePaginator</code> 相当于 <code v-pre>paginate</code> 方法。</p>
<blockquote>
<p><strong>注意</strong><br>
手动创建分页器实例时，你应该手动「切片」传递给分页器的结果数组。如果你不确定如何执行此操作，请查看 <a href="https://secure.php.net/manual/en/function.array-slice.php" target="_blank" rel="noopener noreferrer">array_slice</a> PHP 函数。</p>
</blockquote>
<h3 id="自定义分页的-url" tabindex="-1"><a class="header-anchor" href="#自定义分页的-url"><span>自定义分页的 URL</span></a></h3>
<p>默认情况下，分页器生成的链接会匹配当前的请求 URL。不过，分页器的 withPath 方法允许你自定义分页器生成链接时使用的 URL。比如说，你想要分页器生成类似 <a href="https://example.com/admin/users?page=N" target="_blank" rel="noopener noreferrer">example.com/admin/users?page=N</a> 的链接，你应该给 <code v-pre>withPath</code> 方法传递 <code v-pre>/admin/users</code> 参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">withPath</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/admin/users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="附加参数到分页链接" tabindex="-1"><a class="header-anchor" href="#附加参数到分页链接"><span>附加参数到分页链接</span></a></h4>
<p>你可以使用 <code v-pre>appends</code> 方法向分页链接中添加查询参数。例如，要在每个分页链接中添加 <code v-pre>sort=votes</code> ，你应该这样调用 <code v-pre>appends</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">appends</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'sort'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'votes'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想要把当前所有的请求查询参数添加到分页链接，你可以使用 <code v-pre>withQueryString</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withQueryString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="附加-hash-片段" tabindex="-1"><a class="header-anchor" href="#附加-hash-片段"><span>附加 hash 片段</span></a></h4>
<p>如果你希望向分页器的 URL 添加「哈希片段」，你可以使用 <code v-pre>fragment</code> 方法。例如，你可以使用 <code v-pre>fragment</code> 方法，为 <code v-pre>#user</code> 添加分页链接：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">fragment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="显示分页结果" tabindex="-1"><a class="header-anchor" href="#显示分页结果"><span>显示分页结果</span></a></h2>
<p>当调用 <code v-pre>paginate</code> 方法时， 你会得到一个 <code v-pre>Illuminate\Pagination\LengthAwarePaginator</code> 实例， 而调用 <code v-pre>simplePaginate</code> 方法时，会得到一个 <code v-pre>Illuminate\Pagination\Paginator</code> 实例。 最后，调用 <code v-pre>cursorPaginate</code> 方法，会得到 <code v-pre>Illuminate\Pagination\CursorPaginator</code> 实例。</p>
<p>这些对象提供了数个方法来获取结果集的信息。除了这些辅助方法外，分页器的实例是迭代器，可以像数组一样遍历。所以，你可以使用 <a href="https://learnku.com/docs/laravel/10.x/blade" target="_blank" rel="noopener noreferrer">Blade</a> 模板来显示数据、渲染分页链接等：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;div class=&quot;container&quot;&gt;</span>
<span class="line">    @foreach ($users as $user)</span>
<span class="line">        {{ $user-&gt;name }}</span>
<span class="line">    @endforeach</span>
<span class="line">&lt;/div&gt;</span>
<span class="line"></span>
<span class="line">{{ $users-&gt;links() }}</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>links 方法会渲染结果集中剩余页面的链接。每个链接都包含了 page 查询字符串变量。请记住，links 方法生成的 HTML 兼容 <a href="https://tailwindcss.com/" target="_blank" rel="noopener noreferrer">Tailwind CSS 框架</a> 。</p>
<h3 id="调整分页链接窗口" tabindex="-1"><a class="header-anchor" href="#调整分页链接窗口"><span>调整分页链接窗口</span></a></h3>
<p>在使用分页器展示分页链接时，将展示当前页及当前页面前后各三页的链接。如果有需要，你可以通过 <code v-pre>onEachSide</code> 方法来控制每侧显示多少个链接：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">{{ $users-&gt;onEachSide(5)-&gt;links() }}</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="将结果转换为json" tabindex="-1"><a class="header-anchor" href="#将结果转换为json"><span>将结果转换为JSON</span></a></h3>
<p>Laravel 分页器类实现了 <code v-pre>Illuminate\Contracts\Support\Jsonable</code> 接口契约，提供了 <code v-pre>toJson</code> 方法。这意味着你可以很方便地将分页结果转换为 JSON。你也可以通过直接在路由闭包或者控制器方法中返回分页实例来将其转换为 JSON：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>分页器生成的 JSON 会包括诸如 <code v-pre>total</code>，<code v-pre>current_page</code>，<code v-pre>last_page</code>等元数据信息。实际结果对象将通过 JSON 数组的 <code v-pre>data</code> 键提供。以下是通过自路由中分页器实例的方式创建 JSON 的例子：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">   <span class="token string double-quoted-string">"total"</span><span class="token punctuation">:</span> <span class="token number">50</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"per_page"</span><span class="token punctuation">:</span> <span class="token number">15</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"current_page"</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"last_page"</span><span class="token punctuation">:</span> <span class="token number">4</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"first_page_url"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"http://laravel.app?page=1"</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"last_page_url"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"http://laravel.app?page=4"</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"next_page_url"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"http://laravel.app?page=2"</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"prev_page_url"</span><span class="token punctuation">:</span> <span class="token constant">null</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"path"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"http://laravel.app"</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"from"</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"to"</span><span class="token punctuation">:</span> <span class="token number">15</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"data"</span><span class="token punctuation">:</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">{</span></span>
<span class="line">            <span class="token comment">// 分页数据...</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">{</span></span>
<span class="line">            <span class="token comment">// 分页数据...</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">   <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="自定义分页视图" tabindex="-1"><a class="header-anchor" href="#自定义分页视图"><span>自定义分页视图</span></a></h2>
<p>默认情况下，分页器渲染的视图与 <a href="https://tailwindcss.com/" target="_blank" rel="noopener noreferrer">Tailwind CSS</a> 兼容。不过，如果你并非使用 Tailwind，你也可以自由地定义用于渲染这些链接的视图。在调用分页器实例的 <code v-pre>links</code> 方法时，将视图名称作为第一个参数传递给该方法：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">{{ $paginator-&gt;links('view.name') }}</span>
<span class="line"></span>
<span class="line">&lt;!-- 向视图传递参数... --&gt;</span>
<span class="line">{{ $paginator-&gt;links('view.name', ['foo' =&gt; 'bar']) }}</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>不过，最简单的自定义分页视图的方法依然是使用 <code v-pre>vendor:publish</code> 命令将它们导出到 <code v-pre>resources/views/vendor</code> 目录：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan vendor:publish <span class="token parameter variable">--tag</span><span class="token operator">=</span>laravel-pagination</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>这个命令将会把分页视图导出到 <code v-pre>resources/views/vendor/pagination</code> 目录。该目录下的 <code v-pre>tailwind.blade.php</code> 文件就是默认的分页视图。你可以通过编辑这一文件来自定义分页视图。</p>
<p>如果你想要定义不同的文件作为默认的分页视图，你可以在 <code v-pre>App\Providers\AppServiceProvider</code> 服务提供者中的 <code v-pre>boot</code> 方法内调用 <code v-pre>defaultView</code> 和 <code v-pre>defaultSimpleView</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Providers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Pagination<span class="token punctuation">\</span>Paginator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>ServiceProvider</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">AppServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 引导应用程序服务</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Paginator</span><span class="token operator">::</span><span class="token function">defaultView</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view-name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token class-name static-context">Paginator</span><span class="token operator">::</span><span class="token function">defaultSimpleView</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view-name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="使用-bootstrap" tabindex="-1"><a class="header-anchor" href="#使用-bootstrap"><span>使用 Bootstrap</span></a></h3>
<p>Laravel 同样包含使用 <a href="https://getbootstrap.com/" target="_blank" rel="noopener noreferrer">Bootstrap CSS</a> 构建的分页视图。要使用这些视图来替代默认的 Tailwind 视图，你可以在 <code v-pre>App\Providers\AppServiceProvider</code> 服务提供者中的 <code v-pre>boot</code> 方法内调用分页器的 <code v-pre>useBootstrapFour</code> 或 <code v-pre>useBootstrapFive</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Pagination<span class="token punctuation">\</span>Paginator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导应用程序服务</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Paginator</span><span class="token operator">::</span><span class="token function">useBootstrapFive</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token class-name static-context">Paginator</span><span class="token operator">::</span><span class="token function">useBootstrapFour</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="分页器实例方法" tabindex="-1"><a class="header-anchor" href="#分页器实例方法"><span>分页器实例方法</span></a></h2>
<p>每一个分页器实例都提供了下列方法来获取分页信息：</p>
<table>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>$paginator-&gt;count()</code></td>
<td>获取分页的总数据</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;currentPage()</code></td>
<td>获取当前页码</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;firstItem()</code></td>
<td>获取结果集中第一个数据的编号</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;getOptions()</code></td>
<td>获取分页器选项</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;getUrlRange($start, $end)</code></td>
<td>创建指定页数范围的 URL</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;hasPages()</code></td>
<td>是否有足够多的数据来创建多个页面</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;hasMorePages()</code></td>
<td>是否有更多的页面可供展示</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;items()</code></td>
<td>获取当前页的数据项</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;lastItem()</code></td>
<td>获取结果集中最后一个数据的编号</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;lastPage()</code></td>
<td>获取最后一页的页码（在<code v-pre>simplePaginate</code>中不可用）</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;nextPageUrl()</code></td>
<td>获取下一页的 URL</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;onFirstPage()</code></td>
<td>当前页是否为第一页</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;perPage()</code></td>
<td>获取每一页显示的数量总数</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;previousPageUrl()</code></td>
<td>获取上一页的 URL</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;total()</code></td>
<td>获取结果集中的数据总数（在 <code v-pre>simplePaginate</code>中不可用）</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;url($page)</code></td>
<td>获取指定页的 URL</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;getPageName()</code></td>
<td>获取用于储存页码的查询参数名</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;setPageName($name)</code></td>
<td>设置用于储存页码的查询参数名</td>
</tr>
</tbody>
</table>
<h2 id="游标分页器实例方法" tabindex="-1"><a class="header-anchor" href="#游标分页器实例方法"><span>游标分页器实例方法</span></a></h2>
<p>每一个分页器实例都提供了下列额外方法来获取分页信息:</p>
<table>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>$paginator-&gt;count()</code></td>
<td>获取当前页的数据总数</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;cursor()</code></td>
<td>获取当前分页实例</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;getOptions()</code></td>
<td>获取分页参数选项</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;hasPages()</code></td>
<td>判断是否有足够数据用于分页</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;hasMorePages()</code></td>
<td>判断数据存储是否还有更多项目</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;getCursorName()</code></td>
<td>获取用于查询实例的变量名称</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;items()</code></td>
<td>获取当前页面的数据项目</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;nextCursor()</code></td>
<td>获取下一页数据实例</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;nextPageUrl()</code></td>
<td>获取下一页URL</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;onFirstPage()</code></td>
<td>判断页面是否属于第一页</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;perPage()</code></td>
<td>每页显示的数据数量</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;previousCursor()</code></td>
<td>获取上一页数据实例</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;previousPageUrl()</code></td>
<td>获取上一页URL</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;setCursorName()</code></td>
<td>设置用于查询实例的变量名称</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;url($cursor)</code></td>
<td>获取指定实例的 URL</td>
</tr>
</tbody>
</table>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/10.x/pagination/14884" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/pa...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/10.x/pagination/14884" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/pa...</a></p>
</blockquote>
</div></template>


